package com.zy.algorithm.v2.sort;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zy.algorithm.v2.R;
import com.zy.algorithm.v2.RadixSortUtils;
import com.zy.algorithm.v2.SortUtilsV2;
import com.zy.algorithm.v2.adapter.CountSortAdapter;
import com.zy.algorithm.v2.adapter.RadixSortAdapter;
import com.zy.algorithm.v2.bean.CountSimpleNode;
import com.zy.algorithm.v2.bean.RadixSimpleNode;
import com.zy.algorithm.v2.bean.SimpleNode;
import com.zy.view.LineSetView;

import java.util.List;


//基数排序
public class RadixSortFragment extends BaseSortFragmentV2 {

    RadixSortAdapter radixSortAdapter = new RadixSortAdapter();
    private LineSetView lineSetViewV2;

    @Override
    protected int getLayout() {
        return R.layout.count_sort_layout_v2;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RecyclerView recyclerView = view.findViewById(R.id.base_sort_layout_v2_count_rv);
        recyclerView.setAdapter(radixSortAdapter);
        recyclerView.setLayoutManager(new GridLayoutManager(this.getContext(), 20));

        lineSetViewV2 = view.findViewById(R.id.base_sort_layout_v2_line_set_v2);
        lineSetViewV2.setFactor(0.5f);
        lineSetView.setFactor(0.5f);
    }

    @Override
    protected int[] getEnds(SimpleNode simpleNode) {
        if (simpleNode instanceof RadixSimpleNode) {
            return ((RadixSimpleNode) simpleNode).getSortedS();
        }
        return super.getEnds(simpleNode);
    }

    @Override
    protected LineSetView getEndLineSetView() {
        return lineSetViewV2;
    }

    @Override
    protected String getTitle() {
        return "基数排序";
    }

    @Override
    List<? extends SimpleNode> getDataList() {
        return RadixSortUtils.radix_sort(400, 1000);
    }

    @Override
    protected void play(SimpleNode curNode) {
        super.play(curNode);
        if (curNode instanceof RadixSimpleNode) {
            RadixSimpleNode node = (RadixSimpleNode) curNode;
            radixSortAdapter.setNode(node);
            if (node.getSortedS() != null) {
                lineSetViewV2.setData(node.getSortedS(), node.getOpIndex(), node.getSortedIndex());
            }
        }
    }
}
